package com.eunion.web.admin.entity.spec;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.eunion.web.admin.entity.TableColumnDic;



public class TableColumnDicSpec {
    public static Specification<TableColumnDic> build(String query) {
        return new TableColumnDicQuery(query);
    }

    private static class TableColumnDicQuery implements Specification<TableColumnDic> {

        private String query;
        private TableColumnDicQuery(String query) {
            this.query = query;
        }

        @Override
        public Predicate toPredicate(Root<TableColumnDic> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = getPredicates(root, query, cb, this.query);
            Predicate[] preList = new Predicate[list.size()];
            return cb.and(list.toArray(preList));
        }

    }

    private static List<Predicate> getPredicates(Root<TableColumnDic> root, CriteriaQuery<?> query, CriteriaBuilder cb,String queryArgs) {
        List<Predicate> list = new ArrayList<>();
        if(StringUtils.isNotBlank(queryArgs)) {
            list.add(cb.equal(root.get("columnName"),queryArgs));
        }
        return list;
    }
}
